package com.rightsidetech.http.controller.agentAdmin.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.SysUpdateStatusReq;
import com.rightsidetech.system.dtos.request.agent.*;
import com.rightsidetech.system.dtos.response.agent.SysAgentInfoResp;
import com.rightsidetech.system.service.SysAdminAgentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Author: yhr
 * @Date: 2021-11-01 15:57
 */
@RestController
@RequestMapping("/agentAdmin/sys_agent_admin")
@ShenyuSpringMvcClient(path = "/agentAdmin/sys_agent_admin/**")
@Api(tags = "代理商后台帐号管理")
public class SysAgentLoginInfoController extends BaseController {
    @Resource
    private SysAdminAgentService sysAdminAgentService;

    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("账号列表")
    @GetMapping("/page")
    public ResponseData<PageInfo<SysAgentInfoResp>> selectPage(SysAgentInfoPageReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAdminAgentService.pageAgent(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("添加帐号")
    @PostMapping("/saveAgentInfo")
    @ModifyLog(serviceClass = SysAdminAgentService.class,modifyType = ModifyName.SAVE)
    public ResponseData saveAgentInfo(@RequestBody SysAgentAdminInfoAddReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAdminAgentService.saveAgentAdminInfo(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("编辑帐号")
    @PostMapping("/updateAgentInfo")
    @ModifyLog(serviceClass = SysAdminAgentService.class)
    public ResponseData updateAgentInfo(@RequestBody SysAgentInfoUpdateReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAdminAgentService.updateAgentInfo(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("删除帐号")
    @PostMapping("/del")
    @ModifyLog(serviceClass = SysAdminAgentService.class)
    public ResponseData updateAgentInfo(@RequestBody DelReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAdminAgentService.delAgentInfo(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @ApiOperation("编辑帐号状态")
    @PostMapping("/updateStatus")
    @ModifyLog(serviceClass = SysAdminAgentService.class)
    public ResponseData updateAgentInfoStatus(@RequestBody SysUpdateStatusReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAdminAgentService.updateAgentInfoStatus(req,loginInfoId);
    }

    @Log
    @Token
    @ApiOperation("代理商姓名下拉")
    @GetMapping("/selectList")
    public ResponseData selectList() throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAdminAgentService.selectList(loginInfoId);
    }
}
